package com.link.base.base.accnt.controller;

import com.link.base.user.model.CoreUser;
import com.link.base.base.accnt.model.AccountTemp;
import com.link.base.base.accnt.service.AccountImpService;
import com.link.core.cllog.LCLogger;
import com.link.core.basic.annotations.JsonParam;
import com.link.core.basic.controller.BasicController;
import com.link.core.basic.model.CallParam;
import com.link.core.basic.query.Filter;
import com.link.core.basic.service.BasicService;
import com.link.core.basic.service.KeyGenerateService;
import com.link.core.basic.service.ServiceException;
import com.link.core.util.AppConstants;
import com.link.core.util.StringUtils;
import com.link.core.util.UserUtil;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 客户数据效验导入
 *
 * @author unknown
 * @version 1.0
 * @date: 2018/11/7 9:56
 */

@Controller
@RequestMapping("/link/accntImp")
public class AccountImportController extends BasicController<AccountTemp> {

    @Resource
    private AccountImpService accountImpService;
    @Resource
    private KeyGenerateService keyGenerateService;

    @Override
    public BasicService<AccountTemp> getBasicService() throws Exception {
        return accountImpService;
    }

    /**
     * 更新前操作
     *
     * @param record
     * @param request
     */

    @Override
    public void beforUpsert(AccountTemp record, HttpServletRequest request) {
        //新建标志
        String newInsert = "NEW";
        //更新标志
        String toUpdate = "UPDATE";
        if (StringUtils.equals(record.getRow_status(), newInsert)) {
            Long rowId = keyGenerateService.keyGenerate();
            record.setId(rowId);
            if (StringUtils.isNull(record.getAcctCode())) {
                //客户编码默认等于ID
                record.setAcctCode(rowId.toString());
            }
            record.setAcctTypeLovType(AppConstants.ACCT_TYPE);
        } else if (StringUtils.equals(record.getRow_status(), toUpdate)) {
            record.setAcctTypeLovType(AppConstants.ACCT_TYPE);
        } else {
            throw new ServiceException("PUBLIC-030");
        }
    }

    /**
     * 查询全部前操作
     *
     * @param entity
     * @param request
     * @throws Exception
     */

    @Override
    public void beforQueryAllOrExample(AccountTemp entity, HttpServletRequest request) throws Exception {
        CoreUser user = UserUtil.getUser();
        Filter filter = new Filter("createdBy", String.valueOf(user.getId()));
        List<Filter> filterList = new ArrayList<Filter>();
        filterList.add(filter);
        entity.setFilterList(filterList);
    }

    /**
     * 效验数据
     *
     * @param callParam
     * @param session
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "/check", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> check(@JsonParam CallParam callParam, HttpSession session, HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            accountImpService.runAccountCheck(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }

    /**
     * 插入数据
     *
     * @param callParam
     * @param session
     * @param request
     * @param response
     * @return
     */

    @RequestMapping(value = "/accntImpInsert", method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> accntImpInsert(@JsonParam CallParam callParam, HttpSession session,
                                              HttpServletRequest request, HttpServletResponse response) {
        Map<String, Object> result = new HashMap<String, Object>(4);
        try {
            accountImpService.runNewAccountForimp(callParam);
            result.put("success", true);
        } catch (Exception e) {
            LCLogger.withException(e);
            result.put("success", false);
            result.put("result", e.getMessage());
        }
        return result;
    }
}
